package leetcode100;

// TODO 递归+回溯+剪枝 [经典] 【-】 组合总和
// TODO https://leetcode.cn/problems/combination-sum/solution/shou-hua-tu-jie-zu-he-zong-he-combination-sum-by-x/

import java.util.ArrayList;
import java.util.List;

public class lc100_0039 {

    List<List<Integer>> res = new ArrayList<>();
    List<Integer> path = new ArrayList<>();

    public List<List<Integer>> combinationSum(int[] candidates, int target) {
        dfs(0, 0, candidates, target);
        return res;
    }

    public void dfs(int start, int sum, int[] nums, int target) {
        // 递归结束控制器
        if (sum > target)
            return;
        if (sum == target) {
            res.add(new ArrayList<>(path));
        }
        
        for (int i=start; i<nums.length; i++) {
            path.add(nums[i]);
            dfs(i, sum + nums[i], nums, target);
            path.remove(path.size() - 1);
        }
    }
    
}
